package com.example.zqm.git72.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zqm.git72.JavaBean.User;
import com.example.zqm.git72.Model.UserModel;
import com.example.zqm.git72.R;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private EditText tel;
    private EditText password;
    private Button btn_login,btn_reg;
    public  User user;
    String user_id;
    String error;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_reg.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        initViews();
        initEvents();
    }
    public void initViews()
    {
        btn_login=(Button) findViewById(R.id.btn_login);
        tel =(EditText) findViewById(R.id.tel);
        password=(EditText) findViewById(R.id.password);

    }
    public void initEvents()
    {

        btn_login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                UserModel userModel =new UserModel();
                if("".equals(tel.getText().toString()) || "".equals(password.getText().toString())) {
                    Toast.makeText(LoginActivity.this, "请填写用户名密码！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Call<User> call=userModel.login(tel.getText().toString(),
                        password.getText().toString()
                );
                Callback<User> callback=new Callback<User>(){
                    @Override
                    public void onResponse(Call<User> call, Response<User> response) {
                        User user= response.body();
                        //Log.d("view", login.getError());
                        if ("用户名或密码不正确".equals(user.getError())){

                            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                        }else if (user!=null){

                            Toast.makeText(LoginActivity.this,user.getUser_name(),Toast.LENGTH_SHORT).show();
                            Toast.makeText(LoginActivity.this,"登陆成功", Toast.LENGTH_SHORT).show();
//                            int userID = Integer.parseInt(user.getId());
//                            BaseActivity.userId = String.valueOf(userID);
                            saveUser(user.getId());
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                    }
                };
                call.enqueue(callback);

            }
        });
    }
    public void saveUser(String user_id){
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("user_name",tel.getText().toString());
        //int userid=Integer.parseInt(user_id);
        editor.putString("user_id",user_id);
        editor.putString("password",password.getText().toString());
        editor.commit();
    }
}


